/*
*James Bienz(JB)
*Library CRUD MuscleGroup in the Database.
*
*GNU GPL v2
*/

package com.ist412.pocke.SQL;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ist412.pocke.DTO.MuscleGroupDTO;

public class MuscleGroup {
	
	//TableName
	private static final String TABLE_NAME = "MuscleGroup";
	
	//MuscleGroup Indices
	private static final int ID_INDEX = 0;
	private static final int NAME_INDEX = 1;
	private static final int MUSCLE_DIRECTION_INDEX = 2;
	
	//MuscleGroup Keys
	private static final String NAME_KEY = "name";
	private static final String MUSCLE_DIRECTION_KEY = "muscleDirection";

	public static ArrayList<MuscleGroupDTO> getMuscleGroups(Context context){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		ArrayList<MuscleGroupDTO> muscleGroups = new ArrayList<MuscleGroupDTO>();
		String Query = "SELECT * FROM MuscleGroup";
		Cursor cur = db.rawQuery(Query, null);		
		while(cur.moveToNext()){
			MuscleGroupDTO muscleGroup = new MuscleGroupDTO();
			muscleGroup.setId(cur.getInt(ID_INDEX));
			muscleGroup.setName(cur.getString(NAME_INDEX));
			muscleGroup.setMuscleDirection(cur.getString(MUSCLE_DIRECTION_INDEX));
			muscleGroups.add(muscleGroup);
		}
		return muscleGroups;
	}
	
	public static MuscleGroupDTO getMuscleGroup(Context context, int id){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getReadableDatabase();
		String query = "SELECT * FROM MuscleGroup WHERE id=?";
		Cursor cur = db.rawQuery(query, new String[] {String.valueOf(id)});
		MuscleGroupDTO muscleGroup = new MuscleGroupDTO();
		if(cur.moveToFirst()){
			muscleGroup.setId(cur.getInt(ID_INDEX));
			muscleGroup.setName(cur.getString(NAME_INDEX));
			muscleGroup.setMuscleDirection(cur.getString(MUSCLE_DIRECTION_INDEX));
		}
		return muscleGroup;
	}
	
	public static void createMuscleGroup(Context context, MuscleGroupDTO muscleGroup){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(NAME_KEY, muscleGroup.getName());
		values.put(MUSCLE_DIRECTION_KEY, muscleGroup.getMuscleDirection());
		db.insert(TABLE_NAME, null, values);
	}
	
	public static void updateMuscleGroup(Context context, MuscleGroupDTO muscleGroup){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(NAME_KEY, muscleGroup.getName());
		values.put(MUSCLE_DIRECTION_KEY, muscleGroup.getMuscleDirection());
		String filter = "id=" + muscleGroup.getId();
		db.update(TABLE_NAME, values, filter, null);
	}
	
	public static int deleteMuscleGroup(Context context, int id){
		SQLMachine sql = new SQLMachine(context);
		SQLiteDatabase db = sql.getWritableDatabase();
		String whereClause = "id=" + id;
		return db.delete(TABLE_NAME, whereClause, null);
	}
}
